<!--
 * createor: zc
 * email: zc8023zs@163.com
 * create_time: 2020/3/14 13:46:24
 *-->
<template>
  <div class="app-container">  
     <!-- <div class="filter-container">
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" v-show="permissions.p_add" icon="el-icon-edit" @click="handleCreate">
            {{ $t('table.add') }}
        </el-button> 
    </div>  -->
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      border
      row-key="id">
      <el-table-column
      show-overflow-tooltip
        prop="menu_name"
        label="菜单名称"
        width="180">
      </el-table-column>
      <el-table-column
      show-overflow-tooltip
        prop="link_url"
        label="菜单路径"
        width="300">
      </el-table-column>
      <el-table-column
        prop="sort"
        align="center"
        label="排序" width="80">
      </el-table-column> 
      <el-table-column
        prop="iconCls"
        label="图标" width="80">
      </el-table-column> 
      <el-table-column
        prop="router_path"
        label="路由路径" width="180">
      </el-table-column> 
      <el-table-column
        prop="menu_type"
        label="菜单类别" width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.menu_type==1">
              <font color="green">菜单</font>
            </span>
            <span v-if="scope.row.menu_type==2">
              <font color="blue">详情</font>
            </span> 
          </template>
      </el-table-column> 
      <el-table-column
        prop="user_type"
        label="适用类别">
          <template slot-scope="scope">
            <span v-if="scope.row.user_type==0">
              <font color="green">通用</font>
            </span>
            <span v-if="scope.row.user_type==1">
              <font color="green">管理端</font>
            </span>
            <span v-if="scope.row.user_type==2">
              <font color="blue">门店端</font>
            </span> 
          </template>
      </el-table-column> 
      <!-- <el-table-column v-if="permissions.operation" :label="$t('table.actions')" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary"  v-show="permissions.p_edit" size="mini" @click="handleUpdate(row)">
            {{ $t('table.edit') }}
          </el-button> 
          <el-button  size="mini"  v-show="permissions.p_del" type="danger" @click="handleDelete(row)">
            {{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <!-- <el-dialog :title="textMap[dialogStatus]" :close-on-click-modal='false' :visible.sync="dialogFormVisible" append-to-body>
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" >
        <el-form-item label="菜单名称" prop="menu_name">
          <input type="hidden" v-model="temp.id" />
          <el-input maxlength="50" v-model.trim="temp.menu_name" />
        </el-form-item>  
        <el-form-item label="上级菜单">
          <treeselect
              :options="menuTreeData"
              :default-expand-level="1"
              placeholder="请选择上级参数"
              v-model="temp.pid"
              :normalizer="normalizer"
            />
        </el-form-item>  
        <el-form-item label="菜单路径" prop="link_url">
          <el-input maxlength="301" v-model.trim="temp.link_url"/>
        </el-form-item>   
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="temp.sort" :max="1000" :min="0"/>
        </el-form-item>   
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          {{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>   -->
  </div>
</template>

<script>
import { fetchList, fetchObj, createObj, updateObj, getPMenu, deleteObj } from '@/api/project/system/project_sys_sys_menu'
import { Loading } from 'element-ui';

// import the component
import treeselect from "@riophae/vue-treeselect";
// import the styles
import "@riophae/vue-treeselect/dist/vue-treeselect.css"

export default {
  name: 'a_systema_sys_menu',
  components: { treeselect },
  filters: {
  },
  data() {
      return {
        tableKey: 0,
        temp: {
          id: 0,
          menu_name: '',
          link_url: '',
          sort: 0,
          level: 1,
          pid: null
        },
        permissions:{
          p_select:false,
          p_add:false,
          p_edit: false,
          p_del: false,
          operation: false,
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '添加'
        }, 
        rules: {
          menu_name: [{ required: true, message: '菜单名称必填', trigger: 'change' }],
        },  
        tableData:  [],
        menuTreeData:null
      }
  },
  created() { 
    this.getList();
    this.$per(result=>{ 
      this.permissions.p_select=result['*/system/sysmenu/getSysMenuList']!=null;
      this.permissions.p_add=result['*/system/sysmenu/addSysMenu']!=null;
      this.permissions.p_edit=result['*/system/sysmenu/updateSysMenu']!=null;
      this.permissions.p_del=result['*/system/sysmenu/deleteSysMenu']!=null;
      if(result['*/system/sysmenu/updateSysMenu']!=null || result['*/system/sysmenu/deleteSysMenu']!=null){
            this.permissions.operation=true;
      }
    })
  },
  methods: {  
    openFullScreen() {
        const loading = this.$loading({
          lock: true,
          text: '数据处理中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.2)'
        }); 
        setTimeout(() => {
          loading.close();
        }, 2000);
    },
     normalizer(node) {
      if (node.children == undefined || node.children == null || node.children.length==0) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.menu_name,
        children: node.children
      };
    },
    getList() { 
      fetchList().then(response => {
        this.tableData = response.dataArray.dataList
        this.menuTreeData = response.dataArray.dataList; 
      })
    },
    resetTemp() {
      this.temp = {
        id: 0,
        menu_name: '',
        link_url: '',
        level: 1,
        sort: 0,
        pid: null,
        pname: '顶级菜单'
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {  
          this.openFullScreen();
          createObj(this.temp).then(res => {
            this.dialogFormVisible = false;
            this.getList();
            if (res.code == 0) {
              this.$notify({
                title: "成功",
                message: res.msg,
                type: "success",
                duration: 2000
              });
            }else{
              this.$notify({
                title: "失败",
                message: res.msg,
                type: "error",
                duration: 2000
              });
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      if(this.temp.pid==0 || this.temp.pid=='' || this.temp.pid==undefined){
        this.temp.pid=null;
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) { 
          this.openFullScreen();
          const tempData = Object.assign({}, this.temp); 
          tempData.children='';
          updateObj(tempData).then(res => { 
            this.dialogFormVisible = false;
            this.getList();
            if (res.code == 0) {
              this.$notify({
                title: "成功",
                message: res.msg,
                type: "success",
                duration: 2000
              });
            }else{
              this.$notify({
                title: "失败",
                message: res.msg,
                type: "error",
                duration: 2000
              });
            }
          })
        }
      })
    },
    handleDelete(row) { 
      this.$confirm('确定删除该菜单？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.openFullScreen();
        deleteObj(row).then(res => { 
          this.dialogFormVisible = false;
          this.getList();
          if (res.code == 0) {
              this.$notify({
                title: "成功",
                message: res.msg,
                type: "success",
                duration: 2000
              });
            }else{
              this.$notify({
                title: "失败",
                message: res.msg,
                type: "error",
                duration: 2000
              });
            }
       })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
  }
}
</script>